﻿//https://leetcode.cn/problems/n-queens/?envType=study-plan-v2&envId=top-100-liked

class Solution {
    bool checkCol[10], checkDig1[20], checkDig2[20];
    vector<string> path;
    vector<vector<string>> ret;
    int n;
public:
    vector<vector<string>> solveNQueens(int _n)
    {
        n = _n;
        path.resize(n);
        for (int i = 0; i < n; i++)
            path[i].append(n, '.');

        dfs(0);
        return ret;

    }

    void dfs(int row)
    {
        if (row == n)
        {
            ret.push_back(path);
            return;
        }

        for (int col = 0; col < n; col++)
        {
            //剪枝
            if (!checkCol[col] && !checkDig1[col - row + n] && !checkDig2[col + row])
            {
                path[row][col] = 'Q';
                checkCol[col] = checkDig1[col - row + n] = checkDig2[col + row] = true;
                dfs(row + 1);
                checkCol[col] = checkDig1[col - row + n] = checkDig2[col + row] = false;
                path[row][col] = '.';

            }
        }

    }
};